package com.fanghuaiming.lcn.order.service.impl;

import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.fanghuaiming.lcn.common.model.account.Account;
import com.fanghuaiming.lcn.common.model.item.Sku;
import com.fanghuaiming.lcn.order.dao.mapper.OrderInfoMapper;
import com.fanghuaiming.lcn.order.dao.model.OrderInfo;
import com.fanghuaiming.lcn.order.service.OrderService;
import com.fanghuaiming.lcn.order.spi.account.LcnAccountFeignClient;
import com.fanghuaiming.lcn.order.spi.item.LcnItemFeignClient;
import java.math.BigDecimal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/****
 * @description: 订单服务接口实现类
 * @version:1.0.0
 * @author fanghuaiming
 * @data Created in 2020/11/27 下午1:59
 *
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    /**
     * 商品mapper
     */
    @Autowired
    private OrderInfoMapper orderInfoMapper;

    /**
     * 账户微服务SPI
     */
    @SuppressWarnings("all")
    @Autowired
    private LcnAccountFeignClient fescarAccountFeignClient;

    /**
     * 商品微服务SPI
     */
    @SuppressWarnings("all")
    @Autowired
    private LcnItemFeignClient fescarItemFeignClient;

    /**
     * @Description: 下订单服务
     *
     * @param:
     * @return:
     * @auther: fanghuaiming
     * @date: 2020/11/27 下午2:51
     * @version:1.0.0
     */
    @SuppressWarnings("all")
    @Override
    @LcnTransaction
    public void placeOrder(OrderInfo orderInfo) {
        log.info("=================开始请求-订单微服务-placeOrder下订单接口Service接口=================");
        log.info("=================开始生成订单=================");
        //1、生成订单
        orderInfoMapper.addOrder(orderInfo);
        log.info("=================生成订单完成=================");

        log.info("=================开始请求-商品微服务-扣减商品库存=================");
        //2、扣减库存(跨服务事务)
        Sku sku = new Sku();
        sku.setId(1L);
        sku.setNum(1);
        String result = fescarItemFeignClient.reduceItem(sku);
        log.info("=================商品微服务-扣减商品库存完成=================");
        log.info("=================开始请求-账户微服务-扣减账户余额=================");
        if(orderInfo.getUserId() == 2){
            //模拟异常
            int i = 1/0;
        }
        //3、扣减账户余额(跨服务事务)
        Account account = new Account();
        account.setUserId(1L);
        account.setMoney(new BigDecimal(8000));
        fescarAccountFeignClient.reduceAccount(account);
        if(orderInfo.getUserId() == 3){
            //模拟异常
            int i = 1/0;
        }
        log.info("=================账户微服务-扣减账户余额完成=================");
    }

    /**
     * @Description: 测试SPI
     *
     * @param:
     * @return:
     * @auther: fanghuaiming
     * @date: 2020/11/27 下午5:41
     * @version:1.0.0
     */
    @Override
    public void testSpi() {
        log.info("=================开始请求-订单微服务-testSpi调用其他服务测试spi的Service接口=================");
        Sku sku = new Sku();
        sku.setId(1L);
        sku.setNum(1);
        String result = fescarItemFeignClient.reduceItem(sku);
        log.info(result);
        log.info("=================请求-订单微服务-testSpi调用其他服务测试spi的Service接口完成=================");
    }
}
